﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMUSER : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No = string.Empty; 
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMUSER()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
           string SQL = "select c.name role_name,b.role_id, a.*"
              + "  from am_sys_user a"
              + "  left join am_sys_user_role b on a.id = b.user_id"
              + "  left join am_sys_role c on b.role_id = c.id"
              + "  where 1 = 1";
            DataRowView drv = (DataRowView)uiCBfstorageflag.SelectedItem;
            if ((drv != null)&&(uiCBfstorageflag.SelectedIndex>0))
            {
                string sfstorageflag = drv.Row["id"].ToString();
                SQL = SQL + " and c.id='" + sfstorageflag + "'";
            }
            if (qiid.Text != "")
            {
                SQL = SQL + " and ((a.id like '%" + qiid.Text + "%') or (a.username like '%" + qiid.Text + "%'))";
            }
            SQL = SQL + "  order by c.id,a.id";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            
            rowindex = -1;
        }

        private void FMUSER_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys11.Text = "授权"; sys11.Visible = true;

            //下拉框
            DataTable myGroup = myCache.doGetDict("AM_SYS_ROLE", "");
            uirole_id.DataSource = myGroup;
            uirole_id.DisplayMember = "name";
            uirole_id.ValueMember = "id";

            DataRow dr = myGroup.NewRow();
            dr["name"] = "请选择";
            dr["id"] = "0";
            myGroup.Rows.InsertAt(dr,0);
            uiCBfstorageflag.DataSource = myGroup;
            uiCBfstorageflag.DisplayMember = "name";
            uiCBfstorageflag.ValueMember = "id";
            uiCBfstorageflag.SelectedIndex = 0;

            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=1000");
            uistatus.DataSource = myTs_Dict;
            uistatus.DisplayMember = "fDictName";
            uistatus.ValueMember = "fDictNo";

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uiid.Text = "";
            uiusername.Text = "";
            uipassword_clear.Text = "8888";
            uifcard.Text = "";
            uiemail.Text = "";
            uiphone.Text = "";
            uistatus.SelectedIndex = 1;
            uirole_id.SelectedIndex = 0;

            sType = "A";
            uiid.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uiid.Text = uiDGView["id", rowindex].Value.ToString();
                uiusername.Text = uiDGView["username", rowindex].Value.ToString();
                uipassword_clear.Text = uiDGView["password_clear", rowindex].Value.ToString();
                uifcard.Text = uiDGView["fcard", rowindex].Value.ToString();
                uiemail.Text = uiDGView["email", rowindex].Value.ToString();
                uiphone.Text = uiDGView["phone", rowindex].Value.ToString();

                int i = 0;
                string sstatus = uiDGView["status", rowindex].Value.ToString();
                uistatus.SelectedValue = sstatus;
                foreach (System.Data.DataRowView dr in uistatus.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sstatus))
                    {
                        uistatus.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string srole_id = uiDGView["role_id", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uirole_id.Items)
                {
                    string id = dr["id"].ToString();
                    if (id.Equals(srole_id))
                    {
                        uirole_id.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            sType = "B";

            doLoadData();

            
            uiid.ReadOnly = true;

            base.OnSys02();
        }


        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            sType = "V";
            doLoadData();
            
            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uiid, "请输入编号")
                   && CheckEmpty(uiusername, "请输入姓名")
                   && CheckEmpty(uipassword_clear,"请输入密码")
                   && CheckEmpty(uistatus, "请选择状态")
                   && CheckEmpty(uirole_id, "请选择角色");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return; 
            }
            DataRowView drv1 = (DataRowView)uistatus.SelectedItem;
            string sstatus = drv1.Row["fDictNo"].ToString();

            DataRowView drv2 = (DataRowView)uirole_id.SelectedItem;
            string srole_id = drv2.Row["id"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        am_sys_user myData = context.am_sys_user.Find(int.Parse(uiid.Text));
                        if (myData is null)
                        {
                            am_sys_user myData2 = new am_sys_user();
                            myData2.id = int.Parse(uiid.Text);
                            myData2.username = uiusername.Text;
                            myData2.create_id =int.Parse(sUser);
                            myData2.create_time = DateTime.Now;
                            myData2.email = uiemail.Text;
                            myData2.password = uipassword_clear.Text;
                            myData2.password_clear = uipassword_clear.Text;
                            myData2.phone = uiphone.Text;
                            myData2.update_id = int.Parse(sUser);
                            myData2.update_time = DateTime.Now;
                            myData2.fsite_no_belong = sSite_No;
                            myData2.fcard = uifcard.Text;
                            myData2.status = int.Parse(sstatus);
                            context.am_sys_user.Add(myData2);
                            context.SaveChanges();
                            doUpCache("AM_SYS_USER");
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已经存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    am_sys_user mysys_user = context.am_sys_user.Find(int.Parse(uiid.Text));

                    mysys_user.username = uiusername.Text;
                    mysys_user.username = uiusername.Text;
                    mysys_user.email = uiemail.Text;
                    mysys_user.password = uipassword_clear.Text;
                    mysys_user.password_clear = uipassword_clear.Text;
                    mysys_user.phone = uiphone.Text;
                    mysys_user.update_id = int.Parse(sUser);
                    mysys_user.update_time = DateTime.Now;
                    mysys_user.status = int.Parse(sstatus);
                    mysys_user.fcard = uifcard.Text;
                    int result = context.SaveChanges();
                    doUpCache("AM_SYS_USER");
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            string SQL = string.Empty;
            StaticClass.myDB.SQLList.Clear();

            SQL = "delete from am_sys_user_role where user_id="+uiid.Text;
            StaticClass.myDB.SQLList.Add(SQL);

            SQL = "insert into am_sys_user_role(user_id,role_id)"
              +" values(" + uiid.Text+","+srole_id+")";
            StaticClass.myDB.SQLList.Add(SQL);

            bool IsOk = false;
            string sError = string.Empty;
            StaticClass.myDB.doSave(out IsOk,out sError);

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        //授权
        protected override void OnSys11()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            string suserid = uiDGView["id", rowindex].Value.ToString();
            string susername = uiDGView["username", rowindex].Value.ToString();


            FMUSERBOX myuserbox = new FMUSERBOX();
            myuserbox.suserid = suserid;
            myuserbox.susername = susername;
            myuserbox.myADO = StaticClass.myDB;
            myuserbox.ShowDialog();
            myuserbox.Dispose();

        }

        private void uiCBfstorageflag_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
